namespace IAM.Interface.Model
{
    using IAM.Interface.Common;
    using IAM.Interface.Model.Equals;
    using IAM.Interface.Model.Interface;
    using System;
    using System.Collections;
    using System.Text;
    using System.Xml;

    public class Delegations : AbstractConvertObject, IConvertXml
    {
        private ArrayList _compareresultlist;
        private ArrayList _delegations = new ArrayList();

        public override object ConvertToObject()
        {
            foreach (XmlNode xn in this.CurrentXmlNode.ChildNodes)
            {
                Delegation delegation = new Delegation();
                delegation.CurrentXmlNode = xn;
                delegation = (Delegation) delegation.ConvertToObject();
                this.Items.Add(delegation);
            }
            return this;
        }

        public ArrayList GetCompareResultList(Delegations source, Delegations obj)
        {
            CompareResult cr = null;
            this._compareresultlist = new ArrayList();
            XmlDocument xdsource = new XmlDocument();
            xdsource.LoadXml(source.XML);
            XmlDocument xdobj = new XmlDocument();
            xdobj.LoadXml(obj.XML);
            this._compareresultlist = new ArrayList();
            foreach (object oneofsourceitems in source.Items)
            {
                Delegation sourcedelegation = (Delegation) oneofsourceitems;
                string key = "";
                key = EnDeCoding.Encode(sourcedelegation.Module);
                XmlNode xnobject = xdobj.SelectSingleNode("//Delegation[@Module='" + key + "']");
                if (xnobject == null)
                {
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = true;
                    cr.InObject = false;
                    cr.ContenctEquation = false;
                    this.CompareresultList.Add(cr);
                }
                else
                {
                    Delegation objectACIS = new Delegation();
                    objectACIS.CurrentXmlNode = xnobject;
                    objectACIS.ConvertToObject();
                    if (!sourcedelegation.Equals(objectACIS))
                    {
                        cr = new CompareResult();
                        cr.ID = key;
                        cr.InSource = true;
                        cr.InObject = true;
                        cr.ContenctEquation = false;
                        this.CompareresultList.Add(cr);
                    }
                }
            }
            foreach (object oneofobj in obj.Items)
            {
                Delegation objectACI = (Delegation) oneofobj;
                string key = "";
                key = EnDeCoding.Encode(objectACI.Module);
                XmlNode xnsource = xdsource.SelectSingleNode("//Delegation[@Module='" + key + "']");
                if (xnsource == null)
                {
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = false;
                    cr.InObject = true;
                    cr.ContenctEquation = false;
                    this.CompareresultList.Add(cr);
                    continue;
                }
                Delegation sourACIS = new Delegation();
                sourACIS.CurrentXmlNode = xnsource;
                sourACIS.ConvertToObject();
                if (!objectACI.Equals(sourACIS))
                {
                    foreach (object objresult in this.CompareresultList)
                    {
                        if (((CompareResult) objresult).ID == key)
                        {
                            this.CompareresultList.Remove(objresult);
                            break;
                        }
                    }
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = true;
                    cr.InObject = true;
                    cr.ContenctEquation = false;
                    this.CompareresultList.Add(cr);
                }
            }
            if (this.CompareresultList.Count > 0)
            {
                return this.CompareresultList;
            }
            return null;
        }

        public ArrayList CompareresultList
        {
            get
            {
                return this._compareresultlist;
            }
        }

        public int Count
        {
            get
            {
                return this.Items.Count;
            }
        }

        public ArrayList Items
        {
            get
            {
                return this._delegations;
            }
            set
            {
                this._delegations = value;
            }
        }

        public string XML
        {
            get
            {
                return (this.XMLBoday + this.XMLFinis);
            }
        }

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append(" <Delegations");
                _xml.Append(" Count=\"" + this.Count + "\"");
                _xml.Append(">");
                foreach (object obj in this.Items)
                {
                    _xml.Append(((Delegation) obj).XML);
                }
                return _xml.ToString();
            }
        }

        public string XMLFinis
        {
            get
            {
                return "</Delegations>";
            }
        }
    }
}

